package com.hongshan.wallet.core.trading

import android.app.AlertDialog
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.view.LayoutInflater
import com.alibaba.android.arouter.facade.annotation.Route
import com.hongshan.wallet.BR
import com.hongshan.wallet.Constant
import com.hongshan.wallet.R
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.bean.OrderDescBean
import com.hongshan.wallet.router.RouterIndex
import kotlinx.android.synthetic.main.appbar.*
import kotlinx.android.synthetic.main.dialog_receive_confirm.view.*

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.trading
 * @author   : zhangxx
 * @date     : 2019/3/28
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
@Route(path = RouterIndex.ORDER_DESC)
class OrderDescActivity: MyActivity<ViewDataBinding, OrderDescViewModel>() {

    private val dialogView by lazy { LayoutInflater.from(this).inflate(R.layout.dialog_receive_confirm, null) }
    private val alertDialog by lazy { AlertDialog.Builder(this).setView(dialogView).create() }

    override fun initVariableId() = BR.vm

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_order_desc

    override fun initViewModel(): OrderDescViewModel = ViewModelProviders.of(this)[OrderDescViewModel::class.java]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tvTitle.text = "订单详情"
        val orderId = intent.getIntExtra(Constant.ROUTE_PARAMS_KEY.ORDER_ID,0)
        viewModel.getInfo(orderId)
        viewModel.showReceiveConfrimDialog.observe(this, Observer { showReceiveDialog(it) })
        viewModel.confirmEnable.observe(this, Observer { dialogView.tvConfirm.isEnabled = it == true })
    }

    override fun onDestroy() {
        super.onDestroy()
        viewModel.job?.cancel()
    }

    fun showReceiveDialog(it: OrderDescBean?) {
        alertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        dialogView.tvMoney.text = it?.investor_price
        dialogView.tvAccount.text = it?.account
        dialogView.tvCancle.setOnClickListener {
            alertDialog.dismiss()
        }
        dialogView.tvConfirm.setOnClickListener {
            viewModel.receiveConfirm()
        }
        alertDialog.show()
    }
}